package com.guilin.controller;

import com.guilin.common.core.controller.BaseController;
import com.guilin.common.core.domain.AjaxResult;
import com.guilin.common.core.page.TableDataInfo;
import com.guilin.common.utils.SecurityUtils;
import com.guilin.common.utils.StringUtils;
import com.guilin.common.utils.poi.ExcelUtil;
import com.guilin.domain.Alumni;
import com.guilin.dto.AlumniPasswordDTO;
import com.guilin.system.service.AlumniLoginService;
import com.guilin.system.service.AlumniService;
import com.guilin.system.service.QQToKenService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 校友信息Controller
 * 
 * @author xiaoqi
 * @date 2025-06-07
 */
@RestController
@RequestMapping("/system/alumni")
@Slf4j
public class AlumniController extends BaseController
{
    @Autowired
    private AlumniService alumniService;


    /**
     * 查询校友信息列表
     */
    @PreAuthorize("@ss.hasPermi('admin-management:alumni:list')")
    @GetMapping("/list")
    public TableDataInfo list(Alumni alumni)
    {
        startPage();
        List<Alumni> list = alumniService.selectAlumniList(alumni);
        return getDataTable(list);
    }

    /**
     * 导出校友信息列表
     */
    @PreAuthorize("@ss.hasPermi('admin-management:alumni:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Alumni alumni)
    {
        List<Alumni> list = alumniService.selectAlumniList(alumni);
        ExcelUtil<Alumni> util = new ExcelUtil<Alumni>(Alumni.class);
        util.exportExcel(response, list, "校友信息数据");
    }

    /**
     * 获取校友信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('admin-management:alumni:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(alumniService.selectAlumniById(id));
    }

    /**
     * 新增校友信息
     */
    @PreAuthorize("@ss.hasPermi('admin-management:alumni:add')")
    @PostMapping
    public AjaxResult add(@RequestBody Alumni alumni)
    {

        //校验用户名是否唯一
        if(!alumniService.checkUsernameUnique(alumni))
        {
            return error("新增校友信息'" + alumni.getUsername() + "'失败，校友信息已存在");
        }

        //校验手机号码是否唯一
        else if (StringUtils.isNotEmpty(alumni.getPhone()) &&!alumniService.checkPhoneUnique(alumni))
        {
            return error("新增校友信息'" + alumni.getUsername() + "'失败，手机号码已存在");
        }

        //校验邮箱是否唯一
        else if (StringUtils.isNotEmpty(alumni.getEmail()) &&!alumniService.checkEmailUnique(alumni))
        {
            return error("新增校友信息'" + alumni.getUsername() + "'失败，邮箱账号已存在");
        }

        //对明文密码加密
        alumni.setPassword(SecurityUtils.encryptPassword(alumni.getPassword()));

        return toAjax(alumniService.insertAlumni(alumni));
    }

    /**
     * 修改校友信息
     */
    @PreAuthorize("@ss.hasPermi('admin-management:alumni:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody Alumni alumni)
    {
        //判断密码是否修改
        if(StringUtils.isNotNull(alumni.getPassword()))
        {
            alumni.setPassword(SecurityUtils.encryptPassword(alumni.getPassword()));
        }
        return toAjax(alumniService.updateAlumni(alumni));
    }

    /**
     * 删除校友信息
     */
    @PreAuthorize("@ss.hasPermi('admin-management:alumni:remove')")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(alumniService.deleteAlumniByIds(ids));
    }

    /**
     * 校友状态修改
     */
    @PreAuthorize("@ss.hasPermi('admin-management:alumni:edit')")
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody Alumni alumni)
    {
        return toAjax(alumniService.updateAlumniStatus(alumni));
    }

 }
